let api = require('../../Datas/api.js');
var app = getApp();
Page({
  data: {
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    showView: false,
    textShow:true,
    disabled:true,
    searchVal: "",
    category: [],
    list: {},
    searchIsEmpty: false,
    searchList: {
      page: 1,
      isLast: 0,
      data: []
    },
  },
  //搜索
  bindSearch: function (e) {
    this.setData({
      searchVal: e.detail.value,
      searchList: {
        page: 1,
        isLast: 0,
        data: []
      }
    });
    this.search();
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function () {
    var $this = this;

    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        $this.setData({
          winHeight: calc
        });
      }
    });

    //获取类别
    this.getCategory();

  },
  footerTap: app.footerTap,
  showMask: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView),
      textShow: (!that.data.textShow),
      disabled: (!that.data.disabled)
    })
  },
  getCategory: function () {
    let param = {
      API_URL: app.globalData.apiUrl + 'videoData/getCategory',
      data: {}
    };

    api.result(param).then(data => {
      let datas = data.data.data;

      datas.map((val, i) => (
        this.getCourseByGid(val.id)
      ));

      this.setData({
        category: datas,
      })

    }).catch(e => {
      console.log(e)
      // this.setData({
      //   loadtxt: '数据加载异常',
      //   loading: false
      // })

    })
  },

  search: function (page = 1) {
  
    let param = {
      API_URL: app.globalData.apiUrl + 'videoData/getCourseList',
      data: {
        page: page,
        limit: 20,
        course_title: this.data.searchVal,
      }
    };

    api.result(param).then(data => {
      let datas = data.data.data || [];
      let searchList = this.data.searchList;
      searchList['data'] = datas.concat(searchList.data);

      if (searchList['data'].length) {
        this.setData({
          searchIsEmpty: false,
        })
        
      }else{
        this.setData({
          searchIsEmpty: true,
        })
      }

      if (datas.length < param.data.limit) {
        searchList['isLast'] = 1;
      }

      searchList['page'] = page;

      this.setData({
        searchList: searchList,
      })

    }).catch(e => {
      console.log(e)
      // this.setData({
      //   loadtxt: '数据加载异常',
      //   loading: false
      // })

    })
  },

  getCourseByGid: function (gid, page = 1) {
    console.log('getCourseByGid')
    let param = {
      API_URL: app.globalData.apiUrl + 'videoData/getCourseByGid',
      data: {
        page: page,
        limit: 20,
        gid: gid
      }
    };

    api.result(param).then(data => {
      let datas = data.data.data;
      let isLast = 0;
      if (datas.length < param.data.limit) {
        isLast = 1
      }

      let list = { ...this.data.list };
      let item = list[gid] || [];
      item = item['data'] || [];

      item = item.concat(datas);
      list[gid] = { data: item, page: page, isLast: isLast };

      this.setData({
        list: list,
      });

    }).catch(e => {
      console.log(e)
      // this.setData({
      //   loadtxt: '数据加载异常',
      //   loading: false
      // })

    })

  },

  lower: function (e) {
    let data = e.target.dataset;

    if (data.islast == 0) {
      this.getCourseByGid(data.id, data.page + 1);
    }
  },
  searchLower: function (e) {
    let data = e.target.dataset;

    if (data.islast == 0) {
      this.search(data.page + 1);
    }
  }
})